﻿using System;
using System.IO;
using Newtonsoft.Json;
using System.Windows.Forms;

namespace PrintApp
{
    public partial class MainWindow : Form
    {
        internal static string ip = "200.129.43.219";
        internal static int port = 9090;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void mainForm_Load(object sender, EventArgs e)
        {
            foreach (var file in Directory.GetFiles("\\My Documents", "*.pdf"))
            {
                lvwFiles.Items.Add(new ListViewItem(file) { ImageIndex = 0 });
            }
        }

        private void largeMenuItem_Click(object sender, EventArgs e)
        {
            lvwFiles.View = View.LargeIcon;
            largeMenuItem.Checked = true;
            smallMenuItem.Checked = false;
            listMenuItem.Checked = false;
        }

        private void smallMenuItem_Click(object sender, EventArgs e)
        {
            lvwFiles.View = View.SmallIcon;
            smallMenuItem.Checked = true;
            largeMenuItem.Checked = false;
            listMenuItem.Checked = false;
        }

        private void listMenuItem_Click(object sender, EventArgs e)
        {
            lvwFiles.View = View.List;
            listMenuItem.Checked = true;
            largeMenuItem.Checked = false;
            smallMenuItem.Checked = false;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                var fileName = Path.GetFileName(lvwFiles.Items[lvwFiles.SelectedIndices[0]].Text);
                string jsonText = JsonConvert.SerializeObject(new Message("print.great", fileName));

                var client = new TCPNetworkClient(ip, port);
                client.SendMessage(jsonText);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    ex.Message, 
                    "Error", 
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1);
            }
        }
    }
}